package com.jxl.dnd.charactercreator.controllers
{
	import com.jxl.dnd.charactercreator.models.CharacterModel;
	import com.jxl.dnd.charactercreator.vo.CharacterSheetVO;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.SharedObject;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	
	import mx.collections.ArrayCollection;

	public class Controller extends EventDispatcher
	{
		
		public function loadSavedCharacterSheets():void
		{
			var sheets:Array = [];
			var so:SharedObject = SharedObject.getLocal("charactersheets");
			if(so.data.sheets != null)
			{
				var savedSheets:Array = so.data.sheets;
				var len:int = savedSheets.length;
				var sheetObject:Object;
				var characterSheet:CharacterSheetVO;
				for(var index:uint = 0; index < len; index++)
				{
					sheetObject = savedSheets[index] as Object;
					characterSheet = new CharacterSheetVO();
					characterSheet.buildFromObject(sheetObject);
					sheets[index] = characterSheet;
				}
			}
			CharacterModel.instance.savedCharacterSheets = new ArrayCollection(sheets);
		}
		
		public function deleteCharacterSheet(characterSheet:CharacterSheetVO):void
		{
			var len:int = CharacterModel.instance.savedCharacterSheets.length;
			var sheet:CharacterSheetVO;
			for(var index:uint = 0; index < len; index++)
			{
				sheet = CharacterModel.instance.savedCharacterSheets[index] as CharacterSheetVO;
				if(sheet == characterSheet)
				{
					CharacterModel.instance.savedCharacterSheets.removeItemAt(index);
					saveCharacters();
					return;
				}
			}
		}
		
		public function saveCharacters():void
		{
			var len:int = CharacterModel.instance.savedCharacterSheets.length;
			var sheet:CharacterSheetVO;
			var sheetObject:Object;
			var newSheetsList:Array = [];
			for(var index:uint = 0; index < len; index++)
			{
				sheet = CharacterModel.instance.savedCharacterSheets[index] as CharacterSheetVO;
				sheetObject = sheet.toObject();
				newSheetsList[index] = sheetObject;
			}
			var so:SharedObject = SharedObject.getLocal("charactersheets");
			so.data.sheets = newSheetsList;
			so.flush();
		}
		
		public function saveCharacterSheet(characterSheetVO:CharacterSheetVO):void
		{
			/*
			characterToSave = characterSheetVO;
			if(savedSheets == null)
			{
				savedSheets = new Dictionary(true);
			}
			var file:File;
			var isNew:Boolean = false;
			if(savedSheets[characterSheetVO] == null)
			{
				file = new File();
				savedSheets[characterSheetVO] = file;
				isNew = true;
			}
			else
			{
				file = savedSheets[characterSheetVO];
			}
			
			if(file == null)
			{
				throw new Error("The file is null.");
				return;
			}
			
			file.addEventListener(Event.SELECT, onCharacterSheetSaveFilepathSelected);
			if(isNew == true)
			{
				file.browseForSave("Save Character");
			}
			else
			{
				//saveCharacterStream(file);
			}
			*/
		}
		
		protected function onCharacterSheetSaveFilepathSelected(event:Event):void
		{
			//saveCharacterStream(event.target as File);
		}
		
		/*
		protected function saveCharacterStream(file:File):void
		{
			var stream:FileStream = new FileStream();
		    stream.open(file, FileMode.WRITE);
		    var fileData:String = characterToSave.toXML().toString();
		    fileData = fileData.replace(/\r/g, "\n");
			fileData = fileData.replace(/\n/g, File.lineEnding);
		    stream.writeUTFBytes(fileData);
		    stream.close();
		    characterToSave = null;
		}
		*/
		
		protected var savedSheets:Dictionary;
		protected var characterToSave:CharacterSheetVO;
				
		private static var _inst:Controller;
		
		public static function get instance():Controller
		{
			if(_inst == null) _inst = new Controller();
			return _inst;
		}
		
	}
}